/*
 * CDataStore.h
 *
 *  Created on: 2018年5月11日
 *      Author: zhengboyuan
 */

#ifndef CDATASTORE_H_
#define CDATASTORE_H_

#include "DataStore.h"
#include "SqliteStore.h"
#include "TCriticalSection.h"


class CDataStore: public DataStore
{
public:
    
public:
	CDataStore();
	virtual ~CDataStore();

	virtual bool open(const std::string& path);

	virtual void close();

	virtual bool isOpen();

    virtual bool execute(const std::string& sql);

    virtual bool query(const std::string& sql, std::string& value);

    virtual bool query(const std::string& sql, int& value);

    virtual int update(const std::string& sql);


    virtual bool setFlowCount(const std::string& id, const PersonFlowCount& flowCount);

    virtual bool removeFlowCount(const std::string& id);

    virtual bool getFlowCount(const std::string& id, PersonFlowCount& flowCount);


protected:

    int queryInt(const std::string& sql, int defValue=0);

    bool insert(const std::string& sql, int& autoId);

    bool insert(const std::string& sql, int64_t& autoId);

    template < class T >
    bool queryObject(const std::string& sql, T& t)
    {
    	comn::AutoCritSec lock(m_cs);
    	return m_store.queryObject(sql, t);
    }

    template < class Container >
    int queryObjects(const std::string& sql, Container& cont)
    {
    	comn::AutoCritSec lock(m_cs);
    	return m_store.queryObjects(sql, cont);
    }

	template < class Record >
	int queryObjects(const std::string& sql, Record* records, int size)
	{
		comn::AutoCritSec lock(m_cs);
		return m_store.queryObjects(sql, records, size);
	}


    void createTables();

protected:
	SqliteStore	m_store;
	comn::CriticalSection	m_cs;

};

#endif /* CDATASTORE_H_ */
